package com.winhxd.b2c.common.feign.order;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.constant.ServiceName;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.order.condition.OrderSalesSummaryCondition;
import com.winhxd.b2c.common.domain.order.condition.OrderSummaryCondition;
import com.winhxd.b2c.common.domain.order.condition.OrderUsersCondition;
import com.winhxd.b2c.common.domain.order.vo.OrderBusinessSummaryVO;
import com.winhxd.b2c.common.domain.order.vo.OrderUserBaseSummaryVO;
import com.winhxd.b2c.common.domain.order.vo.OrderUsersProfitListVO;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * @Author lijianhua
 * @Description //订单数据统计
 * @Date 14:45 2019/1/7
 **/
@FeignClient(value = ServiceName.ORDER_SERVICE, fallbackFactory = OrderDataStatisticsServiceFallback.class)
public interface OrderDataStatisticsServiceClient {

    /**
     * @return com.winhxd.b2c.common.domain.ResponseResult<com.winhxd.b2c.common.domain.order.vo.OrderSalesSummaryVO>
     * @Author lijianhua
     * @Description //运营后台首页
     * @Date 15:26 2019/1/7
     * @Param [condition]
     **/
    @RequestMapping(value = "/order/4701/v1/getBusinessSummarySummary", method = RequestMethod.POST)
    ResponseResult<OrderBusinessSummaryVO> getBusinessSummarySummary(@RequestBody OrderSalesSummaryCondition condition);

    /**
     * @Author lijianhua
     * @Description //获取当天预估收益列表
     * @Date 11:33 2019/1/10
     * @Param [condition]
     * @return com.winhxd.b2c.common.domain.ResponseResult<java.util.List<com.winhxd.b2c.common.domain.order.vo.OrderUsersProfitListVO>>
     **/
    @RequestMapping(value = "/order/4702/v1/findOrderUsersProfitList", method = RequestMethod.POST)
    ResponseResult<List<OrderUsersProfitListVO>> findOrderUsersProfitList(OrderUsersCondition condition);

    /**
     * @Author lijianhua
     * @Description //用户基本信息信查询
     * @Date 15:23 2019/1/12
     * @Param [condition]
     * @return com.winhxd.b2c.common.domain.ResponseResult<com.winhxd.b2c.common.domain.order.vo.OrderBusinessSummaryVO>
     **/
    @RequestMapping(value = "/order/4703/v1/getUserBaseSummarySummary", method = RequestMethod.POST)
    ResponseResult<OrderUserBaseSummaryVO> getUserBaseSummarySummary(@RequestBody OrderSummaryCondition condition);

}

@Component
class OrderDataStatisticsServiceFallback implements OrderDataStatisticsServiceClient, FallbackFactory<OrderDataStatisticsServiceClient> {
    private Throwable throwable;
    private static final Logger logger = LoggerFactory.getLogger(OrderDataStatisticsServiceFallback.class);

    public OrderDataStatisticsServiceFallback() {
    }

    public OrderDataStatisticsServiceFallback(Throwable throwable) {
        this.throwable = throwable;
    }

    @Override
    public OrderDataStatisticsServiceClient create(Throwable throwable) {
        return new OrderDataStatisticsServiceFallback(throwable);
    }

    @Override
    public ResponseResult<OrderBusinessSummaryVO> getBusinessSummarySummary(@RequestBody OrderSalesSummaryCondition condition) {
        logger.error("OrderDataStatisticsServiceFallback -> queryBusinessSummarySummary", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<List<OrderUsersProfitListVO>> findOrderUsersProfitList(OrderUsersCondition condition) {
        logger.error("OrderDataStatisticsServiceFallback -> findOrderUsersProfitList", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<OrderUserBaseSummaryVO> getUserBaseSummarySummary(OrderSummaryCondition condition) {
        logger.error("OrderDataStatisticsServiceFallback -> getUserBaseSummarySummary", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }
}